App.require(["matrix3"], function (matrix3) {
    var Matrix3 = matrix3.Matrix3;
    App.defineWidget("rectangle", {
        render: function (ctx) {
            ctx.save();
            var style = this.style;
            ctx.globalAlpha = style.opacity;
            var shadowObj = App.Widget.strToShadow(style.shadow);
            if (shadowObj) {
                ctx.shadowOffsetX = shadowObj.x;
                ctx.shadowOffsetY = shadowObj.y;
                ctx.shadowBlur = shadowObj.blur;
                ctx.shadowColor = shadowObj.color;
            }
            
            var transforms = App.Widget.strToTransform(style.transform);
            if (transforms) {
                for (var i = 0; i < transforms.length; i++) {
                    var tranObj = transforms[i];
                    ctx[tranObj.key].apply(ctx, tranObj.param);
                }
            }
            
            this.createPath(ctx);
            if (style.fill) {
                ctx.fillStyle = style.fill;
                ctx.fill();
            }
            if (style.stroke) {
                ctx.strokeStyle = style.stroke;
                ctx.linewidth = style.linewidth;
                if (Array.isArray(style.linedash) && style.linedash.length > 0) {
                    ctx.setLineDash(style.linedash);
                    ctx.lineDashOffset = style.linedashoffset;
                }
                ctx.stroke();
            }
            ctx.restore();
        },
        createPath: function (ctx) {
            var style = this.style;
            ctx.beginPath();
            ctx.rect(style.x, style.y, style.width, style.height);
        },
        isPointIn: function (ctx, x, y) {
            this.createPath(ctx);
            return ctx.isPointInPath(x, y);
        },
        style: {
            transform: [], 
            width: 100,
            height: 100,
            x: 0,
            y: 0,
            shadow: "0 0 0 black",
            stroke: "",
            fill: "black",
            opacity: 1,
            position: App.PositionOption.LEFTBOTTOM,
            linewidth: 1,
            linedash: "",
            linedashoffset: 0
        },
        get transform() {
            return this.style.transform;
        },
        set transform(val) {
            this.style.transform = val;
            this.getScene().nextRender();
        },
        get width() {
            return this.style.width;
        },
        set width(val) {
            this.style.transform = val;
            this.getScene().nextRender();
        },
        get height() {
            return this.style.height;
        },
        set height(val) {
            this.style.height = val;
            this.getScene().nextRender();
        },
        get x() {
            return this.style.x;
        },
        set x(val) {
            this.style.x = val;
            this.getScene().nextRender();
        },
        get y() {
            return this.style.y;
        },
        set y(val) {
            this.style.y = val;
            this.getScene().nextRender();
        },
        get shadow() {
            return this.style.shadow;
        },
        set shadow(val) {
            this.style.shadow = val;
            this.getScene().nextRender();
        },
        get stroke() {
            return this.style.stroke;
        },
        set stroke(val) {
            this.style.stroke = val;
            this.getScene().nextRender();
        },
        get fill() {
            return this.style.fill;
        },
        set fill(val) {
            this.style.fill = val;
            this.getScene().nextRender();
        },
        get opacity() {
            return this.style.opacity;
        },
        set opacity(val) {
            this.style.opacity = val;
            this.getScene().nextRender();
        },
        get position() {
            return this.style.position;
        },
        set position(val) {
            this.style.position = val;
            this.getScene().nextRender();
        },
        get linewidth() {
            return this.style.linewidth;
        },
        set linewidth(val) {
            this.style.linewidth = val;
            this.getScene().nextRender();
        },
        get linedash() {
            return this.style.linedash;
        },
        set linedash(val) {
            this.style.linedash = val;
            this.getScene().nextRender();
        },
        get linedashoffset() {
            return this.style.linedashoffset;
        },
        set linedashoffset(val) {
            this.style.linedashoffset = val;
            this.getScene().nextRender();
        }
    })
})